package cn.iocoder.yudao.module.resource.controller.admin.interactionlog;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.resource.controller.admin.interactionlog.vo.InteractionLogPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.interactionlog.vo.InteractionLogRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.interactionlog.vo.InteractionLogSaveReqVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.interactionlog.InteractionLogDO;
import cn.iocoder.yudao.module.resource.service.interactionlog.InteractionLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 交互日志")
@RestController
@RequestMapping("/lm/interaction-log")
@Validated
public class InteractionLogController {

    @Resource
    private InteractionLogService interactionLogService;

    @PostMapping("/create")
    @Operation(summary = "创建交互日志")
    @PreAuthorize("@ss.hasPermission('lm:interaction-log:create')")
    public CommonResult<Void> createInteractionLog(@Valid @RequestBody InteractionLogSaveReqVO createReqVO) {
        interactionLogService.asynCreateInteractionLog(createReqVO);
        return success(null);
    }

    @PutMapping("/update")
    @Operation(summary = "更新交互日志")
    @PreAuthorize("@ss.hasPermission('lm:interaction-log:update')")
    public CommonResult<Boolean> updateInteractionLog(@Valid @RequestBody InteractionLogSaveReqVO updateReqVO) {
        interactionLogService.updateInteractionLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除交互日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lm:interaction-log:delete')")
    public CommonResult<Boolean> deleteInteractionLog(@RequestParam("id") String id) {
        interactionLogService.deleteInteractionLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得交互日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lm:interaction-log:query')")
    public CommonResult<InteractionLogRespVO> getInteractionLog(@RequestParam("id") String id) {
        InteractionLogDO interactionLog = interactionLogService.getInteractionLog(id);
        return success(BeanUtils.toBean(interactionLog, InteractionLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得交互日志分页")
    @PreAuthorize("@ss.hasPermission('lm:interaction-log:query')")
    public CommonResult<PageResult<InteractionLogRespVO>> getInteractionLogPage(@Valid InteractionLogPageReqVO pageReqVO) {
        PageResult<InteractionLogDO> pageResult = interactionLogService.getInteractionLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InteractionLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出交互日志 Excel")
    @PreAuthorize("@ss.hasPermission('lm:interaction-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInteractionLogExcel(@Valid InteractionLogPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InteractionLogDO> list = interactionLogService.getInteractionLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "交互日志.xls", "数据", InteractionLogRespVO.class,
                BeanUtils.toBean(list, InteractionLogRespVO.class));
    }

}